import type { VxeGridProps } from '@vben/plugins/vxe-table';

import type { VbenFormProps } from '#/adapter/form';

import { $t } from '@vben/locales';

import { Tag } from 'ant-design-vue';

import { FileBizTypeEnum } from '#/enums/commonEnum';
import { dateUtil } from '#/utils/dateUtil';

// 表格参数定义
export const gridSchemas: VxeGridProps<any> = {
  toolbarConfig: {
    refresh: true, // 刷新
    print: false, // 打印
    export: false, // 导出
    custom: true, // 自定义列
    zoom: true, // 最大化最小化
  },
  columns: [
    { title: $t('common.seq'), type: 'seq', width: 50 },
    {
      field: 'name',
      title: $t('devOperation.application.defApplication.name'),
    },
    {
      field: 'version',
      width: 100,
      title: $t('devOperation.application.defApplication.version'),
    },
    {
      field: 'state',
      width: 150,
      title: '有效期',
      slots: {
        default: ({ row }) => {
          if (row.expirationTime) {
            if (dateUtil(row.expirationTime).isBefore(Date.now())) {
              return <Tag color="error">已过期</Tag>;
            } else if (
              dateUtil(row.expirationTime).isBefore(dateUtil().add(30, 'days'))
            ) {
              const duration = dateUtil.duration(
                dateUtil(row.expirationTime).diff(Date.now()),
              );
              return duration.days() > 0 ? (
                <Tag color="warning">{duration.days() + 1}天后到期</Tag>
              ) : (
                <Tag color="warning">{duration.hours()}小时后到期</Tag>
              );
            } else {
              return <Tag color="processing">{row.expirationTime}</Tag>;
            }
          } else {
            return <Tag color="success">永久有效</Tag>;
          }
        },
      },
    },
    {
      field: 'createdTime',
      formatter: 'formatDateTime',
      width: 180,
      title: $t('common.createdTime'),
    },
    {
      field: 'action',
      fixed: 'right',
      title: '操作',
      slots: { default: 'ACTION' },
      width: 120,
    },
  ],
};
// 搜索表单参数定义
export const searchFormSchemas: VbenFormProps = {
  schema: [
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('devOperation.application.defApplication.name'),
      componentProps: {
        placeholder: '请输入',
        allowClear: true,
      },
    },
  ],
};

// 表单参数定义
export const formSchamas: VbenFormProps = {
  wrapperClass: 'grid-cols-12', // 24栅格,
  commonConfig: {
    formItemClass: 'col-span-12',
  },
  schema: [
    {
      fieldName: 'id',
      label: 'ID',
      component: 'Input',
      ifDetail: false,
      dependencies: {
        show: false,
        triggerFields: ['id'],
      },
    },
    {
      fieldName: 'appendixIcon',
      label: $t('devOperation.application.defApplication.logo'),
      component: 'Avatar',
      componentProps: {
        placeholder: '请选择logo',
        showBtn: false,
        uploadParams: { bizType: FileBizTypeEnum.DEF_APPLICATION_LOGO },
      },
      formItemClass: 'col-span-12',
    },
    {
      fieldName: 'name',
      label: $t('devOperation.application.defApplication.name'),
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
        allowClear: true,
      },
      formItemClass: 'col-span-6',
      rules: 'required',
      detailSpan: 12,
    },
    {
      fieldName: 'version',
      label: $t('devOperation.application.defApplication.version'),
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
        allowClear: true,
      },
      formItemClass: 'col-span-6',
      detailSpan: 12,
    },
    {
      fieldName: 'introduce',
      label: $t('devOperation.application.defApplication.introduce'),
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
        allowClear: true,
      },
      formItemClass: 'col-span-6',
      detailSpan: 12,
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注',
      },
      formItemClass: 'col-span-12',
      detailSpan: 12,
    },

    {
      fieldName: 'url',
      label: $t('devOperation.application.defApplication.url'),
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
        allowClear: true,
      },
      formItemClass: 'col-span-6',
      detailSpan: 12,
    },
    {
      fieldName: 'sortValue',
      label: $t('devOperation.application.defApplication.sortValue'),
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入',
        allowClear: true,
      },
      formItemClass: 'col-span-6',
      detailSpan: 12,
    },
  ],
};
